package com.taotaojs.util.date;

import com.taotaojs.exception.MyInnerException;
import com.taotaojs.util.ConstantUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日历工具类
 * @author: TaoTaojs
 * @Date: 2019年11月19日10:55:04
 * @Description:
 *  - 创建日历实例
 *  - 快速日期格式化方法
 *  - 获取前后某个单位时间的日期
 * @关联类:
 * @参考链接：
 */
public class CalendarUtil {

	public static final String FORMAT_DATE = "yyyy-MM-dd";
	public static final String FORMAT_TIME = "HH:mm:ss";
	public static final String FORMAT_ALL = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 判断Calendar是否需要初始化日期，并创建返回
	 * @param date
	 * @return
	 */
	public static Calendar getCalendar(Date date){
		Calendar calendar = Calendar.getInstance();
		if(date != null){
			calendar.setTime(date);
		}
		return calendar;
	}

	/**
	 * 前几天或者后几天的日期
	 * @param date
	 * @param i 数字0表示为当天23:59:59   1表示明天   -1表示昨天
	 * @return
	 */
	public static Date getLastOrNextDate(Date date, final int i){
		Calendar calendar = CalendarUtil.getCalendar(date);
		calendar.add(Calendar.DAY_OF_YEAR, i);
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		//如果是后几天，那么秒数减一
		if(i >= 0){
			calendar.add(Calendar.DAY_OF_YEAR, 1);
			calendar.add(Calendar.SECOND, -1);
		}
		return calendar.getTime();
	}

	/**
	 * 前几小时或者后几小时的日期
	 * @param date
	 * @param i 数字0表示为当前小时
	 * @return
	 */
	public static Date getLastOrNextHour(Date date, final int i){
		Calendar calendar = CalendarUtil.getCalendar(date);
		calendar.add(Calendar.HOUR_OF_DAY, i);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 前几分钟或者后几分钟的日期
	 * @param date
	 * @param i 数字0表示为当天23:59:59   1表示明天   -1表示昨天
	 * @return
	 */
	public static Date getLastOrNextMinute(Date date, final int i){
		Calendar calendar = CalendarUtil.getCalendar(date);
		calendar.add(Calendar.MINUTE, i);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 使用字符串获取date类型数据
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static Date parseDate(String date, String formatStr) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
		try {
			return dateFormat.parse(date);
		} catch (ParseException e) {
			throw new MyInnerException(e);
		}
	}

	/**
	 * 快速格式化日期方法
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String formatDate(Date date, String formatStr){
		SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
		return dateFormat.format(date);
	}

	/** 获取当前日期 */
	public String getNow(String stringType) {
		Calendar calendar = Calendar.getInstance();
		return new SimpleDateFormat(stringType).format(calendar.getTime());
	}

	/** 获取前/后几天的日期 */
	public String getLastOrNextDate(int i, String stringType) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_YEAR, i);
		return new SimpleDateFormat(stringType).format(calendar.getTime());
	}

	/** 获取这周某天日期 */
	public String getOneDateOfWeek(int i, String stringType) {
		Calendar calendar = Calendar.getInstance();

		if (calendar.get(Calendar.DAY_OF_WEEK) != 1) {
			if (i <= ConstantUtil.SIX) {
				calendar.set(Calendar.DAY_OF_WEEK, i + 1);
			} else if (i == ConstantUtil.SEVEN) {
				calendar.add(Calendar.WEEK_OF_MONTH, 1);
				calendar.set(Calendar.DAY_OF_WEEK, 1);
			}
		} else {
			if (i <= ConstantUtil.SIX) {
				calendar.add(Calendar.WEEK_OF_MONTH, -1);
				calendar.set(Calendar.DAY_OF_WEEK, i + 1);
			} else if (i == ConstantUtil.SEVEN) {
				calendar.set(Calendar.DAY_OF_WEEK, 1);
			}
		}
		return new SimpleDateFormat(stringType).format(calendar.getTime());
	}

	/** 获取前/后第几周日期 */
	public String getLastOrNextWeek(int i, String stringType) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.WEEK_OF_MONTH, i);
		return new SimpleDateFormat(stringType).format(calendar.getTime());
	}

	/** 获取前/后几个月对应日期 */
	public String getlastOrNextDateOfMonth(int i, String stringType) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, i);
		return new SimpleDateFormat(stringType).format(calendar.getTime());
	}

	/** 获取某月某一天日期 */
	public String getOneDateOfMonth(int i, int j, String stringType) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, i);
		calendar.set(Calendar.DAY_OF_MONTH, j);
		return new SimpleDateFormat(stringType).format(calendar.getTime());
	}

	/** 获取某月第一天日期 */
	public String getFirstDateOfMonth(int i, String stringType) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, i);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat(stringType).format(calendar.getTime());
	}

	/** 获取某月最后一天日期 */
	public String getLastDateOfMonth(int i, String stringType) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, i + 1);
		calendar.set(Calendar.DAY_OF_MONTH, 0);
		return new SimpleDateFormat(stringType).format(calendar.getTime());
	}

	/** 获取某月第一天零点日期时间 */
	public Date getFirstDateTimeZoreOfMonth(int i, String stringType) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, i);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat(stringType).parse(new SimpleDateFormat(stringType).format(calendar.getTime()));
	}

	/** 获取前/后几年的日期 */
	public String getlastOrNextDateOfYear(int i, String stringType) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, i);
		return new SimpleDateFormat(stringType).format(calendar.getTime());
	}

	/** 获取某年第几天的日期 */
	public String getOneDateOfYear(int i, int j, String stringType) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, i);
		calendar.set(Calendar.DAY_OF_YEAR, j);
		return new SimpleDateFormat(stringType).format(calendar.getTime());
	}

}
